package com.itheima.web.request;

import java.lang.String;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import static java.net.URLEncoder.encode;


public class URLDemo {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String username = "张三";

        //1.URL编码
        String encode = encode(username, "utf-8");
        System.out.println(encode);

        //2.URL解码
        String decode = URLDecoder.decode(encode, "ISO-8859-1");
//        String decode = URLDecoder.decode(encode, "utf-8");
        System.out.println(decode);

        //3.转换为字节数据
        byte[] bytes = decode.getBytes("ISO-8859-1");
       /* for (byte b : bytes) {
            System.out.print(b + " ");
        }*/

        //4.将字节数组转换为字符串,解码
        //乱码原因：tomcat进行解码的时候，默认的字符集是ISO-8859-1
        String string = new String(bytes, "utf-8");
        System.out.println(string);
    }
}
